package com.peng.test.cursoriterator;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.github.phantomthief.util.CursorIterator;

/**
 * CursorIteratorSample
 * <p>
 * Created on 2022/9/21
 *
 * @author lupeng
 */
public class CursorIteratorSample {


    @Test
    public void t1() {
        CursorIterator.<Long, Bean>newGenericBuilder()
                .bufferSize(100)
                .start(0L)
                .cursorExtractor(Bean::getCreateTime)
                .buildEx((cursor, limit) -> generate(cursor == null ? 0L : cursor, limit))
                .stream()
                .forEach(System.out::println);
    }


    private List<Bean> generate(long cursor, int limit) {
        List<Bean> result = new ArrayList<>();
        for (int i = 0; i < limit; i++) {
            long createTime = System.currentTimeMillis();
            result.add(new Bean(createTime, createTime + "-" + i));
        }
        return result;
    }

    static class Bean {
        private long createTime;
        private String seq;

        public Bean(long createTime, String seq) {
            this.createTime = createTime;
            this.seq = seq;
        }

        @Override
        public String toString() {
            return "Bean{" +
                    "createTime=" + createTime +
                    ", seq=" + seq +
                    '}';
        }

        public long getCreateTime() {
            return createTime;
        }

        public void setCreateTime(long createTime) {
            this.createTime = createTime;
        }

        public String getSeq() {
            return seq;
        }

        public void setSeq(String seq) {
            this.seq = seq;
        }
    }
}
